module.exports=class extends baseController{
    constructor(){
        super();
        this.loginPageLogic=this.loadLogic('iframe/custom-theme/login-page');
        this.router.post('/getItems',this.getItems.bind(this));
        this.router.post('/getItem',this.getItem.bind(this));
        this.router.post('/edit',this.edit.bind(this));
        this.router.post('/del',this.del.bind(this));
        this.router.post('/getItemBySign',this.getItemBySign.bind(this));
        this.router.post('/checkSign',this.checkSign.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let ret={};
        try{
            let FindFilterModel = {};
            let curPage = req.body.CurPage ? Number(req.body.CurPage) : 1;
            let pageSize = req.body.PageSize ? Number(req.body.PageSize) : 20;
            let sort = req.body.Sort ? req.body.Sort : "ct_create_time|desc";
            FindFilterModel.ct_title = req.body.ct_title || '';
            FindFilterModel.ct_sign = req.body.ct_sign || '';
            FindFilterModel.ct_create_time = req.body.ct_create_time || '';
            FindFilterModel.ct_org_id = req.body.ct_org_id ;
            let Ret = await this.loginPageLogic.getItems(FindFilterModel,curPage,pageSize,sort,req.user);
            ret=this.Success(1,Ret);
        }
        catch(err){
            ret=this.Error('操作失败',err);
        }
        res.json(ret);
    }

    async getItem(req,res){
        let Result={};
        try{
            let id = req.body.id || '';
            let ret=await this.loginPageLogic.getItem(id);
            if(!ret){
                Result=this.Error('记录不存在');
            }
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async edit(req,res){
        let Result={};
        try{
            let ret= await this.loginPageLogic.insertOrUpdate(req.body);
            if(ret){
                Result=this.Success(1,ret);
            }else{
                Result=this.Error('记录更新失败');
            }
        }
        catch(err){
            Result=this.Error('记录更新失败',err);
        }
        res.json(Result);
    }

    async del(req,res){
        let Result={};
        try{
            let keys = req.body.keys;
            let ret= await this.loginPageLogic.del(keys);
            Result=this.Success(1);
        }
        catch(err){
            Result=this.Error('记录删除失败',err);
        }
        res.json(Result);
    }

    async getItemBySign(req,res){
        let Result={};
        try{
            let sign = req.body.sign;
            let ret=await this.loginPageLogic.getItemBySign(sign);
            if(!ret){
                Result=this.Error('记录不存在');
            }
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async checkSign(req,res){
        let Result={};
        try{
            let ct_id=req.body.ct_id || 0;
            let ct_sign=req.body.ct_sign || '';
            let count=await this.loginPageLogic.checkSign(ct_id,ct_sign);
            Result=this.Success(1,{count:count});
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }
}